<?php
if(defined("_PARA_THREAD"))
    return;
define("_PARA_THREAD", 1);

require_once('ForumMessage.php');
require_once('ForumMessageBody.php');
require_once('HTMLable.php');

class Thread extends HTMLable {

    public $id;
    public $topics;
    public $status;
    public $changedate;
    public $createdate;
    public $tree = null;
    public $size = 0;


    public function Thread( $id,
                            $topics,
                            $status,
                            $changedate,
                            $createdate,
                            $tree ) 
    {
        $this->id           = $id;
        $this->topics       = $topics;
        $this->status       = $status;
        $this->changedate   = $changedate;
        $this->createdate   = $createdate; 

        if($tree) {
            $this->tree   = $tree;
            $this->size   = $tree->count();            
        }
    }


    public function add_message( $message ) {
        if( $this->tree ) {
            if($this->tree->add_child( $message ) )
                $this->size++;
        }
        elseif( !$message->parent_id && ($message->thread_id == $this->id)) {
            $this->tree = $message;
            $this->size = 1;
            //echo "################################################## $message->id \n";
        }
        else {
            //echo "************************************************** $message->id \n";
        }
    }


    public function add_bodies( $bodies ) {
        if( !$this->tree )
            return false;

        $found_count = 0;
        foreach( $bodies as $body ) {
            $found = $this->tree->find_message( $body->id );
            if($found) {
                $found_count++;
                $found->set_body( $body );
            }
        }

        return $found_count;
    }


    public function find_message( $id ) {
        if( !$this->tree )
            return null;

        return $this->tree->find_message( $id );
    }


    public function htmlize( $options ) {

        if( $options['expanded'] ) {
            echo "<div class=\"para_forum_thread\" id=\"para_forum_thread_$this->id\">\n";

            $oc0 = "onclick=\"para_forum_uev_view_ix()\"";
            echo "<div class=\"para_forum_thread_ctrls\">",
                "<span class=\"para_forum_thread_ctrls_back\" $oc0>Back to Index</span>",
                "</div>";


            $this->tree->htmlize( array('expanded' => true,
                                        'bodies'   => $options['bodies']
                                      ) );

            echo "<div class=\"para_forum_thread_info\" >",
                "Trådstørrelse: $this->size , ",
                "Seneste indlæg i tråden: ", Helpers::nice_timestamp( $this->changedate),
                "</div>\n";
            echo "</div>\n";

        }
        else  {
            echo "<div class=\"para_forum_thread\" id=\"para_forum_thread_$this->id\">\n";

            $this->tree->htmlize( array('expanded'    => false,
                                        'thread_head' => true 
                                      ) 
                );

            echo "<div class=\"para_forum_thread_info\" >",
                "Trådstørrelse: $this->size , ",
                "Seneste indlæg i tråden: ", Helpers::nice_timestamp( $this->changedate ),
                "</div>\n";
            echo "</div>\n";
        }

    }


    public function adm_htmlize( $options ) {

        $opt2 = array( 'thread_head' => true );

        echo '<div class="padm_forum_thread" >' , "\n";
        $this->tree->adm_htmlize( $opt2 );
        echo "</div>\n";
    }

}
?>